package xyz.icxl.android.architecture.ui.mvvm

import android.os.Bundle
import android.view.View
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.LiveData
import xyz.icxl.android.architecture.observer.DslAPIRequestStatus
import xyz.icxl.android.architecture.ui.BaseFragment

abstract class BaseVMFragment<VM : BaseViewModel, DB : ViewDataBinding> : BaseFragment<DB>() {
    abstract val mViewModel: VM
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        if (isFirstRendering)
            registerObserver()
        mViewModel.loadingStatus.observe(viewLifecycleOwner, pageLoadingDialogStatus)
        mViewModel.snackbarStatus.observe(viewLifecycleOwner, pageSnackbarStatusObserver)
        super.onViewCreated(view, savedInstanceState)
    }

    abstract fun registerObserver()


    protected fun <T> LiveData<BaseViewModel.APIRequestStatus<T>>.observeDslApi(listener: DslAPIRequestStatus.ListenerBuilder<T>.() -> Unit) {
        observe(this@BaseVMFragment, DslAPIRequestStatus(listener))
    }
}